<?php

namespace app\admin\controller;

class Product extends Base {

    //put your code here
    public function changeStatus0() {
        $id = input('param.id');
        if ($id) {
            $data = db('goods')->where('goods_id', $id)->update(['goods_status' => 0]);
            $this->redirect('productList');
        }
    }

    public function changeStatus1() {
        $id = input('param.id');
        if ($id) {
            $data = db('goods')->where('goods_id', $id)->update(['goods_status' => 1]);
            $this->redirect('productList');
        }
    }

    public function productModify() {
        $id = input('param.id');
        if ($id) {
            $data = db('goods')->where('goods_id', $id)->find();
            $this->assign('data', $data);
        }
        $arr = input('post.');
        if ($arr) {
            //	获取表单上传文件	例如上传了001.jpg
            $file = request()->file('goods_big_img');
            //	移动到框架应用根目录/public/uploads/	目录下
            if ($file) {

                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    //	成功上传后	获取上传信息
                    //	输出	jpg
//            echo $info->getExtension();
                    //	输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    $img = $info->getSaveName();
                    $arr['goods_big_img'] = '/uploads/' . $img;
                    //	输出 42a79759f284b767dfcb2a0197904287.jpg
//            echo $info->getFilename();
                } else {
                    //	上传失败获取错误信息
                    echo $file->getError();
                    exit;
                }
            }
            db('goods')->update($arr);
            $this->redirect('productList');
        }
        return $this->fetch();
    }

    public function productList() {

        $keywords = input('post.keywords');
        if ($keywords) {
            $data = db('goods')->where('goods_name', 'like', "%$keywords%")->select();
        } else {
            $data = db('goods')->select();
        }
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function del() {
        $id = input('param.id');
        if ($id) {
            db('goods')->delete($id);
        }
        $this->redirect('productList');
    }

    public function add() {
        $data = input('post.');
        if ($data) {
            //	获取表单上传文件	例如上传了001.jpg
            $file = request()->file('goods_big_img');
            //	移动到框架应用根目录/public/uploads/	目录下
            if ($file) {

                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    //	成功上传后	获取上传信息
                    //	输出	jpg
//            echo $info->getExtension();
                    //	输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    $img = $info->getSaveName();
                    $data['goods_big_img'] = '/uploads/' . $img;
                    //	输出 42a79759f284b767dfcb2a0197904287.jpg
//            echo $info->getFilename();
                    //生成缩略图
//                    $data['goods_small_img']=ROOT_PATH .'public/uploads/small/' . $info->getFilename();
                    $goods_small_img='uploads/small/' . $info->getFilename();  //用时要加'/'  。。。
                    $image=\think\Image::open($file);
                    $image->thumb(150,150)->save($goods_small_img);
                    $data['goods_small_img'] = '/'.$goods_small_img;
                    
                    //GD库生成缩略图
//                    $uploadfile=ROOT_PATH .'public'.$data['goods_big_img'];
//                    $data['goods_small_img'] = 'uploads/small/' . $info->getFilename();
//                    $smallimage=$data['goods_small_img'];
//                    $src_image = ImageCreateFromJPEG($uploadfile);
//                    $srcW = ImageSX($src_image); //获得图像的宽 
//                    $srcH = ImageSY($src_image); //获得图像的高 
//                    $dst_image = ImageCreateTrueColor(100,100);
//                    ImageCopyResized($dst_image,$src_image,0,0,0,0,100,100,$srcW,$srcH);
//                    ImageJpeg($dst_image,$smallimage); //创建缩略图文件 
                } else {
                    //	上传失败获取错误信息
                    echo $file->getError();
                    exit;
                }
            }
            $data['goods_create_time'] = date("Y-m-d H:i:s");
            $res = db('goods')->insert($data);
            if ($res) {
                $this->success('商品添加成功', 'product/productList');
            }
        }
        return $this->fetch();
    }

    public function category() {
        return $this->fetch();
    }

    public function categoryAdd() {
        return $this->fetch();
    }

    public function brand() {
        return $this->fetch();
    }

}
